home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / M3OPS.BAS < prev    next >
BASIC Source File  |  1996-03-25  |  19KB  |  633 lines

  1. Attribute VB_Name = "M3Ops"
  2. Option Explicit
  3.  
  4. Global Const PI = 3.14159265358979
  5. Global Const INFINITY = 2147483647
  6.  
  7. Global Const m3Parallel = 0
  8. Global Const m3Perspective = 1
  9.  
  10. Type Point3D
  11.     coord(1 To 4) As Single
  12.     trans(1 To 4) As Single
  13. End Type
  14.  
  15. Type Segment3D
  16.     pt1 As Integer
  17.     pt2 As Integer
  18. End Type
  19.  
  20. ' ***********************************************
  21. ' Convert the spherical coordinates into
  22. ' Cartesian coordinates.
  23. ' ***********************************************
  24. Sub m3SphericalToCartesian(r As Single, theta As Single, phi As Single, x As Single, y As Single, z As Single)
  25. Dim r2 As Single
  26.     
  27.     ' Create a line to the center of projection.
  28.     y = r * Sin(phi)
  29.     r2 = r * Cos(phi)
  30.     x = r2 * Cos(theta)
  31.     z = r2 * Sin(theta)
  32. End Sub
  33. ' ***********************************************
  34. ' Create a transformation matrix for an oblique
  35. ' projection onto the X-Y plane.
  36. ' ***********************************************
  37. Sub m3ObliqueXY(M() As Single, S As Single, theta As Single)
  38.     m3Identity M
  39.     M(3, 1) = -S * Cos(theta)
  40.     M(3, 2) = -S * Sin(theta)
  41.     M(3, 3) = 0
  42. End Sub
  43.  
  44.  
  45. ' ***********************************************
  46. ' Create a transformation matrix for orthographic
  47. ' projection along the X axis.
  48. ' ***********************************************
  49. Sub m3OrthoSide(M() As Single)
  50.     m3Identity M
  51.     M(1, 1) = 0
  52.     M(3, 1) = -1
  53.     M(3, 3) = 0
  54. End Sub
  55. ' ***********************************************
  56. ' Create a transformation matrix for orthographic
  57. ' projection along the Y axis.
  58. ' ***********************************************
  59. Sub m3OrthoTop(M() As Single)
  60.     m3Identity M
  61.     M(2, 2) = 0
  62.     M(3, 2) = -1
  63.     M(3, 3) = 0
  64. End Sub
  65.  
  66. ' ***********************************************
  67. ' Create a transformation matrix for orthographic
  68. ' projection along the Z axis.
  69. ' ***********************************************
  70. Sub m3OrthoFront(M() As Single)
  71.     m3Identity M
  72.     M(3, 3) = 0
  73. End Sub
  74.  
  75. ' ***********************************************
  76. ' Create an identity matrix.
  77. ' ***********************************************
  78. Public Sub m3Identity(M() As Single)
  79. Dim i As Integer
  80. Dim j As Integer
  81.  
  82.     For i = 1 To 4
  83.         For j = 1 To 4
  84.             If i = j Then
  85.                 M(i, j) = 1
  86.             Else
  87.                 M(i, j) = 0
  88.             End If
  89.         Next j
  90.     Next i
  91. End Sub
  92.  
  93. ' ***********************************************
  94. ' Normalize a 3-D point vector.
  95. ' ***********************************************
  96. Public Sub m3NormalizeCoords(x As Single, y As Single, z As Single, S As Single)
  97.     x = x / S
  98.     y = y / S
  99.     z = z / S
  100.     S = 1
  101. End Sub
  102.  
  103. ' ***********************************************
  104. ' Normalize a 3-D point vector.
  105. ' ***********************************************
  106. Public Sub m3NormalizePoint(P() As Single)
  107. Dim i As Integer
  108. Dim value As Single
  109.  
  110.     value = P(4)
  111.     For i = 1 To 3
  112.         P(i) = P(i) / value
  113.     Next i
  114.     P(4) = 1
  115. End Sub
  116.  
  117.  
  118. ' ***********************************************
  119. ' Normalize a 3-D transformation matrix.
  120. ' ***********************************************
  121. Public Sub m3NormalizeMatrix(M() As Single)
  122. Dim i As Integer
  123. Dim j As Integer
  124. Dim value As Single
  125.  
  126.     value = M(4, 4)
  127.     For i = 1 To 4
  128.         For j = 1 To 4
  129.             M(i, j) = M(i, j) / value
  130.         Next j
  131.     Next i
  132. End Sub
  133.  
  134.  
  135.  
  136.  
  137. ' ***********************************************
  138. ' Create a 3-D transformation matrix for a
  139. ' perspective projection along the Z axis onto
  140. ' the X-Y plane with focus at the origin and the
  141. ' center of projection at distance (0, 0, D).
  142. ' ***********************************************
  143. Public Sub m3PerspectiveXZ(M() As Single, D As Single)
  144.     m3Identity M
  145.     If D <> 0 Then M(3, 4) = -1 / D
  146. End Sub
  147.  
  148. ' ***********************************************
  149. ' Create a 3-D transformation matrix for a
  150. ' projection with:
  151. '       center of projection    (cx, cy, cz)
  152. '       focus                   (fx, fy, fx)
  153. '       UP vector               <ux, yx, uz>
  154. ' ptype should be m3Perspective or m3Parallel.
  155. ' ***********************************************
  156. Public Sub m3Project(M() As Single, ptype As Integer, Cx As Single, Cy As Single, Cz As Single, Fx As Single, Fy As Single, Fz As Single, ux As Single, uy As Single, uz As Single)
  157. Static M1(1 To 4, 1 To 4) As Single
  158. Static M2(1 To 4, 1 To 4) As Single
  159. Static M3(1 To 4, 1 To 4) As Single
  160. Static M4(1 To 4, 1 To 4) As Single
  161. Static M5(1 To 4, 1 To 4) As Single
  162. Static M12(1 To 4, 1 To 4) As Single
  163. Static M34(1 To 4, 1 To 4) As Single
  164. Static M1234(1 To 4, 1 To 4) As Single
  165. Dim sin1 As Single
  166. Dim cos1 As Single
  167. Dim sin2 As Single
  168. Dim cos2 As Single
  169. Dim sin3 As Single
  170. Dim cos3 As Single
  171. Dim A As Single
  172. Dim B As Single
  173. Dim C As Single
  174. Dim d1 As Single
  175. Dim d2 As Single
  176. Dim d3 As Single
  177. Dim up1(1 To 4) As Single
  178. Dim up2(1 To 4) As Single
  179.  
  180.     ' Translate the focus to the origin.
  181.     m3Translate M1, -Fx, -Fy, -Fz
  182.  
  183.     A = Cx - Fx
  184.     B = Cy - Fy
  185.     C = Cz - Fz
  186.     d1 = Sqr(A * A + C * C)
  187.     If d1 <> 0 Then
  188.         sin1 = -A / d1
  189.         cos1 = C / d1
  190.     End If
  191.     d2 = Sqr(A * A + B * B + C * C)
  192.     If d2 <> 0 Then
  193.         sin2 = B / d2
  194.         cos2 = d1 / d2
  195.     End If
  196.     
  197.     ' Rotate around the Y axis to place the
  198.     ' center of projection in the Y-Z plane.
  199.     m3Identity M2
  200.     
  201.     ' If d1 = 0 then the center of projection
  202.     ' already lies in the Y axis and thus the Y-Z plane.
  203.     If d1 <> 0 Then
  204.         M2(1, 1) = cos1
  205.         M2(1, 3) = -sin1
  206.         M2(3, 1) = sin1
  207.         M2(3, 3) = cos1
  208.     End If
  209.     
  210.     ' Rotate around the X axis to place the
  211.     ' center of projection in the Z axis.
  212.     m3Identity M3
  213.     
  214.     ' If d2 = 0 then the center of projection
  215.     ' lies at the origin. This makes projection
  216.     ' impossible.
  217.     If d2 <> 0 Then
  218.         M3(2, 2) = cos2
  219.         M3(2, 3) = sin2
  220.         M3(3, 2) = -sin2
  221.         M3(3, 3) = cos2
  222.     End If
  223.     
  224.     ' Apply the rotations to the UP vector.
  225.     up1(1) = ux
  226.     up1(2) = uy
  227.     up1(3) = uz
  228.     up1(4) = 1
  229.     m3Apply up1, M2, up2
  230.     m3Apply up2, M3, up1
  231.  
  232.     ' Rotate around the Z axis to put the UP
  233.     ' vector in the Y-Z plane.
  234.     d3 = Sqr(up1(1) * up1(1) + up1(2) * up1(2))
  235.     m3Identity M4
  236.     
  237.     ' If d3 = 0 then the UP vector is a zero
  238.     ' vector so do nothing.
  239.     If d3 <> 0 Then
  240.         sin3 = up1(1) / d3
  241.         cos3 = up1(2) / d3
  242.         M4(1, 1) = cos3
  243.         M4(1, 2) = sin3
  244.         M4(2, 1) = -sin3
  245.         M4(2, 2) = cos3
  246.     End If
  247.     
  248.     ' Project.
  249.     If ptype = m3Perspective And d2 <> 0 Then
  250.         m3PerspectiveXZ M5, d2
  251.     Else
  252.         m3Identity M5
  253.     End If
  254.  
  255.     ' Combine the transformations.
  256.     m3MatMultiply M12, M1, M2
  257.     m3MatMultiply M34, M3, M4
  258.     m3MatMultiply M1234, M12, M34
  259.     If ptype = m3Perspective Then
  260.         m3MatMultiplyFull M, M1234, M5
  261.     Else
  262.         m3MatMultiply M, M1234, M5
  263.     End If
  264. End Sub
  265.  
  266.  
  267.  
  268. ' ***********************************************
  269. ' Create a 3-D transformation matrix for a
  270. ' perspective projection with:
  271. '       center of projection    (r, phi, theta)
  272. '       focus                   (fx, fy, fx)
  273. '       up vector               <ux, uy, uz>
  274. ' ptype should be m3Perspective or m3Parallel.
  275. ' ***********************************************
  276. Sub m3PProject(M() As Single, ptype As Integer, r As Single, phi As Single, theta As Single, Fx As Single, Fy As Single, Fz As Single, ux As Single, uy As Single, uz As Single)
  277. Dim Cx As Single
  278. Dim Cy As Single
  279. Dim Cz As Single
  280. Dim r2 As Single
  281.  
  282.     ' Convert to Cartesian coordinates.
  283.     Cy = r * Sin(phi)
  284.     r2 = r * Cos(phi)
  285.     Cx = r2 * Cos(theta)
  286.     Cz = r2 * Sin(theta)
  287.     m3Project M, ptype, Cx, Cy, Cz, Fx, Fy, Fz, ux, uy, uz
  288. End Sub
  289.  
  290. ' ***********************************************
  291. ' Create a transformation matrix for reflecting
  292. ' across the plane passing through (p1, p2, p3)
  293. ' with normal vector <n1, n2, n3>.
  294. ' ***********************************************
  295. Sub m3Reflect(M() As Single, p1 As Single, p2 As Single, p3 As Single, n1 As Single, n2 As Single, n3 As Single)
  296. Dim T(1 To 4, 1 To 4) As Single     ' Translate.
  297. Dim R1(1 To 4, 1 To 4) As Single    ' Rotate 1.
  298. Dim r2(1 To 4, 1 To 4) As Single    ' Rotate 2.
  299. Dim S(1 To 4, 1 To 4) As Single     ' Reflect.
  300. Dim R2i(1 To 4, 1 To 4) As Single   ' Unrotate 2.
  301. Dim R1i(1 To 4, 1 To 4) As Single   ' Unrotate 1.
  302. Dim Ti(1 To 4, 1 To 4) As Single    ' Untranslate.
  303. Dim D As Single
  304. Dim L As Single
  305. Dim M12(1 To 4, 1 To 4) As Single
  306. Dim M34(1 To 4, 1 To 4) As Single
  307. Dim M1234(1 To 4, 1 To 4) As Single
  308. Dim M56(1 To 4, 1 To 4) As Single
  309. Dim M567(1 To 4, 1 To 4) As Single
  310.  
  311.     ' Translate the plane to the origin.
  312.     m3Translate T, -p1, -p2, -p3
  313.     m3Translate Ti, p1, p2, p3
  314.  
  315.     ' Rotate around Z-axis until the normal is in
  316.     ' the Y-Z plane.
  317.     m3Identity R1
  318.     D = Sqr(n1 * n1 + n2 * n2)
  319.     R1(1, 1) = n2 / D
  320.     R1(1, 2) = n1 / D
  321.     R1(2, 1) = -R1(1, 2)
  322.     R1(2, 2) = R1(1, 1)
  323.     
  324.     m3Identity R1i
  325.     R1i(1, 1) = R1(1, 1)
  326.     R1i(1, 2) = -R1(1, 2)
  327.     R1i(2, 1) = -R1(2, 1)
  328.     R1i(2, 2) = R1(2, 2)
  329.     
  330.     ' Rotate around the X-axis until the normal
  331.     ' lies along the Y axis.
  332.     m3Identity r2
  333.     L = Sqr(n1 * n1 + n2 * n2 + n3 * n3)
  334.     r2(2, 2) = D / L
  335.     r2(2, 3) = -n3 / L
  336.     r2(3, 2) = -r2(2, 3)
  337.     r2(3, 3) = r2(2, 2)
  338.     
  339.     m3Identity R2i
  340.     R2i(2, 2) = r2(2, 2)
  341.     R2i(2, 3) = -r2(2, 3)
  342.     R2i(3, 2) = -r2(3, 2)
  343.     R2i(3, 3) = r2(3, 3)
  344.  
  345.     ' Reflect across the X-Z plane.
  346.     m3Identity S
  347.     S(2, 2) = -1
  348.  
  349.     ' Combine the matrices.
  350.     m3MatMultiply M12, T, R1
  351.     m3MatMultiply M34, r2, S
  352.     m3MatMultiply M1234, M12, M34
  353.     m3MatMultiply M56, R2i, R1i
  354.     m3MatMultiply M567, M56, Ti
  355.     m3MatMultiply M, M1234, M567
  356. End Sub
  357.  
  358.  
  359. ' ***********************************************
  360. ' Create a transformation atrix for rotating
  361. ' through angle theta around a line passing
  362. ' through (p1, p2, p3) in direction <d1, d2, d3>.
  363. ' Theta is measured counterclockwise as you look
  364. ' down the line opposite the line's direction.
  365. ' ***********************************************
  366. Sub m3LineRotate(M() As Single, p1 As Single, p2 As Single, p3 As Single, d1 As Single, d2 As Single, d3 As Single, theta As Single)
  367. Dim T(1 To 4, 1 To 4) As Single     ' Translate.
  368. Dim R1(1 To 4, 1 To 4) As Single    ' Rotate 1.
  369. Dim r2(1 To 4, 1 To 4) As Single    ' Rotate 2.
  370. Dim Rot3(1 To 4, 1 To 4) As Single  ' Rotate.
  371. Dim R2i(1 To 4, 1 To 4) As Single   ' Unrotate 2.
  372. Dim R1i(1 To 4, 1 To 4) As Single   ' Unrotate 1.
  373. Dim Ti(1 To 4, 1 To 4) As Single    ' Untranslate.
  374. Dim D As Single
  375. Dim L As Single
  376. Dim M12(1 To 4, 1 To 4) As Single
  377. Dim M34(1 To 4, 1 To 4) As Single
  378. Dim M1234(1 To 4, 1 To 4) As Single
  379. Dim M56(1 To 4, 1 To 4) As Single
  380. Dim M567(1 To 4, 1 To 4) As Single
  381.  
  382.     ' Translate the plane to the origin.
  383.     m3Translate T, -p1, -p2, -p3
  384.     m3Translate Ti, p1, p2, p3
  385.  
  386.     ' Rotate around Z-axis until the line is in
  387.     ' the Y-Z plane.
  388.     m3Identity R1
  389.     D = Sqr(d1 * d1 + d2 * d2)
  390.     R1(1, 1) = d2 / D
  391.     R1(1, 2) = d1 / D
  392.     R1(2, 1) = -R1(1, 2)
  393.     R1(2, 2) = R1(1, 1)
  394.     
  395.     m3Identity R1i
  396.     R1i(1, 1) = R1(1, 1)
  397.     R1i(1, 2) = -R1(1, 2)
  398.     R1i(2, 1) = -R1(2, 1)
  399.     R1i(2, 2) = R1(2, 2)
  400.     
  401.     ' Rotate around the X-axis until the line
  402.     ' lies along the Y axis.
  403.     m3Identity r2
  404.     L = Sqr(d1 * d1 + d2 * d2 + d3 * d3)
  405.     r2(2, 2) = D / L
  406.     r2(2, 3) = -d3 / L
  407.     r2(3, 2) = -r2(2, 3)
  408.     r2(3, 3) = r2(2, 2)
  409.     
  410.     m3Identity R2i
  411.     R2i(2, 2) = r2(2, 2)
  412.     R2i(2, 3) = -r2(2, 3)
  413.     R2i(3, 2) = -r2(3, 2)
  414.     R2i(3, 3) = r2(3, 3)
  415.  
  416.     ' Rotate around the line (Y axis).
  417.     m3YRotate Rot3, theta
  418.  
  419.     ' Combine the matrices.
  420.     m3MatMultiply M12, T, R1
  421.     m3MatMultiply M34, r2, Rot3
  422.     m3MatMultiply M1234, M12, M34
  423.     m3MatMultiply M56, R2i, R1i
  424.     m3MatMultiply M567, M56, Ti
  425.     m3MatMultiply M, M1234, M567
  426. End Sub
  427.  
  428. ' ***********************************************
  429. ' Create a 3-D transformation matrix for scaling
  430. ' by scale factors Sx, Sy, and Sz.
  431. ' ***********************************************
  432. Public Sub m3Scale(M() As Single, Sx As Single, Sy As Single, Sz As Single)
  433.     m3Identity M
  434.     M(1, 1) = Sx
  435.     M(2, 2) = Sy
  436.     M(3, 3) = Sz
  437. End Sub
  438.  
  439. ' ***********************************************
  440. ' Create a 3-D transformation matrix for
  441. ' translation by Tx, Ty, and Tz.
  442. ' ***********************************************
  443. Public Sub m3Translate(M() As Single, Tx As Single, Ty As Single, Tz As Single)
  444.     m3Identity M
  445.     M(4, 1) = Tx
  446.     M(4, 2) = Ty
  447.     M(4, 3) = Tz
  448. End Sub
  449.  
  450. ' ***********************************************
  451. ' Create a 3-D transformation matrix for rotation
  452. ' around the X axis (angle measured in radians).
  453. ' ***********************************************
  454. Public Sub m3XRotate(M() As Single, theta As Single)
  455.     m3Identity M
  456.     M(2, 2) = Cos(theta)
  457.     M(3, 3) = M(2, 2)
  458.     M(2, 3) = Sin(theta)
  459.     M(3, 2) = -M(2, 3)
  460. End Sub
  461.  
  462. ' ***********************************************
  463. ' Create a 3-D transformation matrix for rotation
  464. ' around the Y axis (angle measured in radians).
  465. ' ***********************************************
  466. Public Sub m3YRotate(M() As Single, theta As Single)
  467.     m3Identity M
  468.     M(1, 1) = Cos(theta)
  469.     M(3, 3) = M(1, 1)
  470.     M(3, 1) = Sin(theta)
  471.     M(1, 3) = -M(3, 1)
  472. End Sub
  473.  
  474. ' ***********************************************
  475. ' Create a 3-D transformation matrix for rotation
  476. ' around the Z axis (angle measured in radians).
  477. ' ***********************************************
  478. Public Sub m3ZRotate(M() As Single, theta As Single)
  479.     m3Identity M
  480.     M(1, 1) = Cos(theta)
  481.     M(2, 2) = M(1, 1)
  482.     M(1, 2) = Sin(theta)
  483.     M(2, 1) = -M(1, 2)
  484. End Sub
  485.  
  486. ' ************************************************
  487. ' Create a matrix that rotates around the Y axis
  488. ' so the point (x, y, z) lies in the X-Z plane.
  489. ' ************************************************
  490. Sub m3YRotateIntoXZ(Result() As Single, x As Single, y As Single, z As Single)
  491. Dim D As Single
  492.  
  493.     m3Identity Result
  494.     D = Sqr(x * x + y * y)
  495.     Result(1, 1) = x / D
  496.     Result(1, 2) = -y / D
  497.     Result(2, 1) = -Result(1, 2)
  498.     Result(2, 2) = Result(1, 1)
  499. End Sub
  500.  
  501. ' ***********************************************
  502. ' Set copy = orig.
  503. ' ***********************************************
  504. Public Sub m3MatCopy(copy() As Single, orig() As Single)
  505. Dim i As Integer
  506. Dim j As Integer
  507.  
  508.     For i = 1 To 4
  509.         For j = 1 To 4
  510.             copy(i, j) = orig(i, j)
  511.         Next j
  512.     Next i
  513. End Sub
  514.  
  515. ' ************************************************
  516. ' Apply a transformation matrix to a point where
  517. ' the transformation may not have 0, 0, 0, 1 in
  518. ' its final column. Normalize only the X and Y
  519. ' components of the result to preserve the Z
  520. ' information.
  521. ' ************************************************
  522. Public Sub m3ApplyFull(V() As Single, M() As Single, Result() As Single)
  523. Dim i As Integer
  524. Dim j As Integer
  525. Dim value As Single
  526.  
  527.     For i = 1 To 4
  528.         value = 0#
  529.         For j = 1 To 4
  530.             value = value + V(j) * M(j, i)
  531.         Next j
  532.         Result(i) = value
  533.     Next i
  534.     
  535.     ' Renormalize the point.
  536.     ' Note that value still holds Result(4).
  537.     If value <> 0 Then
  538.         Result(1) = Result(1) / value
  539.         Result(2) = Result(2) / value
  540.         ' Do not transform the Z component.
  541.     Else
  542.         ' Make the Z value greater than that of
  543.         ' the center of projection so the point
  544.         ' will be clipped.
  545.         Result(3) = INFINITY
  546.     End If
  547.     Result(4) = 1#
  548. End Sub
  549.  
  550.  
  551.  
  552.  
  553. ' ************************************************
  554. ' Apply a transformation matrix to a point.
  555. ' ************************************************
  556. Public Sub m3Apply(V() As Single, M() As Single, Result() As Single)
  557.     Result(1) = V(1) * M(1, 1) + _
  558.                 V(2) * M(2, 1) + _
  559.                 V(3) * M(3, 1) + M(4, 1)
  560.     Result(2) = V(1) * M(1, 2) + _
  561.                 V(2) * M(2, 2) + _
  562.                 V(3) * M(3, 2) + M(4, 2)
  563.     Result(3) = V(1) * M(1, 3) + _
  564.                 V(2) * M(2, 3) + _
  565.                 V(3) * M(3, 3) + M(4, 3)
  566.     Result(4) = 1#
  567. End Sub
  568.  
  569. ' ************************************************
  570. ' Multiply two matrices together. The matrices
  571. ' may not contain 0, 0, 0, 1 in their last
  572. ' columns.
  573. ' ************************************************
  574. Public Sub m3MatMultiplyFull(Result() As Single, A() As Single, B() As Single)
  575. Dim i As Integer
  576. Dim j As Integer
  577. Dim k As Integer
  578. Dim value As Single
  579.  
  580.     For i = 1 To 4
  581.         For j = 1 To 4
  582.             value = 0#
  583.             For k = 1 To 4
  584.                 value = value + A(i, k) * B(k, j)
  585.             Next k
  586.             Result(i, j) = value
  587.         Next j
  588.     Next i
  589. End Sub
  590. ' ***********************************************
  591. ' Multiply two matrices together.
  592. ' ***********************************************
  593. Public Sub m3MatMultiply(Result() As Single, A() As Single, B() As Single)
  594.     Result(1, 1) = A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1) + A(1, 3) * B(3, 1)
  595.     Result(1, 2) = A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2) + A(1, 3) * B(3, 2)
  596.     Result(1, 3) = A(1, 1) * B(1, 3) + A(1, 2) * B(2, 3) + A(1, 3) * B(3, 3)
  597.     Result(1, 4) = 0#
  598.     Result(2, 1) = A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1) + A(2, 3) * B(3, 1)
  599.     Result(2, 2) = A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2) + A(2, 3) * B(3, 2)
  600.     Result(2, 3) = A(2, 1) * B(1, 3) + A(2, 2) * B(2, 3) + A(2, 3) * B(3, 3)
  601.     Result(2, 4) = 0#
  602.     Result(3, 1) = A(3, 1) * B(1, 1) + A(3, 2) * B(2, 1) + A(3, 3) * B(3, 1)
  603.     Result(3, 2) = A(3, 1) * B(1, 2) + A(3, 2) * B(2, 2) + A(3, 3) * B(3, 2)
  604.     Result(3, 3) = A(3, 1) * B(1, 3) + A(3, 2) * B(2, 3) + A(3, 3) * B(3, 3)
  605.     Result(3, 4) = 0#
  606.     Result(4, 1) = A(4, 1) * B(1, 1) + A(4, 2) * B(2, 1) + A(4, 3) * B(3, 1) + B(4, 1)
  607.     Result(4, 2) = A(4, 1) * B(1, 2) + A(4, 2) * B(2, 2) + A(4, 3) * B(3, 2) + B(4, 2)
  608.     Result(4, 3) = A(4, 1) * B(1, 3) + A(4, 2) * B(2, 3) + A(4, 3) * B(3, 3) + B(4, 3)
  609.     Result(4, 4) = 1#
  610. End Sub
  611.  
  612. ' ***********************************************
  613. ' Compute the cross product of two vectors.
  614. ' Set <x, y, z> = <x1, y1, z1> X <x2, y2, z2>.
  615. ' ***********************************************
  616. Sub m3Cross(x As Single, y As Single, z As Single, x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single)
  617.     x = y1 * z2 - z1 * y2
  618.     y = z1 * x2 - x1 * z2
  619.     z = x1 * y2 - y1 * x2
  620. End Sub
  621.  
  622. ' ***********************************************
  623. ' Give the vector the indicated length.
  624. ' ***********************************************
  625. Sub m3SizeVector(ByVal L As Single, x As Single, y As Single, z As Single)
  626.     L = L / Sqr(x * x + y * y + z * z)
  627.     x = x * L
  628.     y = y * L
  629.     z = z * L
  630. End Sub
  631.  
  632.  
  633.